﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SyncList.Scanner;
using System.Diagnostics;
using System.Threading;
using SyncList.Business.ValueObject;
using SyncList.Business;

namespace SyncList
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        long minSize = 50 * 1024 * 1024;
        string baseFolder = @"L:\FILMS";
        string fileName = @"A:\DEV\SyncList\Lists\Emmanuel\Films.xml";
        string title = "Films";

        private bool _mustStop = false;


        public Window1()
        {
            InitializeComponent();

            this.BtnStop.Click += new RoutedEventHandler(BtnStop_Click);
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
        }



        /// <summary>
        /// Demande d'interruption du traitement
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnStop_Click(object sender, RoutedEventArgs e)
        {
            _mustStop = true;
            BtnStop.IsEnabled = false;
        }


        /// <summary>
        /// Loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Window1_Loaded(object sender, RoutedEventArgs e)
        {                

            ThreadStart start = new ThreadStart(ThreadTask);
            Thread thread = new Thread(start);
            thread.Start();          
        }


        /// <summary>
        /// Exécuté dans le Thread
        /// </summary>
        private void ThreadTask()
        {

            while (!_mustStop)
            {

                IImportService import = ServiceAccessor.ImportService;
                Data data = import.ImportXml(fileName);
                if (data == null)
                    data = new Data();

                data.MinSize = minSize;
                if(!data.PathList.Contains(baseFolder))
                    data.PathList.Add(baseFolder);
                data.Title = title;

                FileFilter filter = new FileFilter();
                filter.MinSize = minSize;

                FileScanner scanner = new FileScanner();
                int count = scanner.Scan(data.Files, baseFolder, filter, 0, new TimeSpan(0, 0, 60), ref _mustStop);
                if (count == 0) break;

                IExportService export = ServiceAccessor.ExportService;

                export.ExportXml(data, fileName, minSize);

                
            }

            if(_mustStop)
                MessageBox.Show("INTERROMPU");
            else
                MessageBox.Show("FIN");

        }

    }
}
